import getConfig from "./config.json";
import data from "./data.json";
import _ from "lodash";
import fs from "fs";
import moment from "moment";

const cxg = [
    '杂鱼sensei也太非了吧，爱丽丝没绷住www',
    '不关爱丽丝的事 不要打爱丽丝...',
    'sensei来抱抱爱丽丝 可能就没那么非啦~',
    'sensei杂鱼~怎么会抽0井1了呢~',
    'sensei不要盒爱丽丝 不关爱丽丝的事的',
    '爱丽丝要飞起来啦 sensei真的好非呀hhhh',
    '不是爱丽丝的错 是阿罗娜把让爱丽丝这样做的....',
    'sensei吃井啦哈哈~杂鱼~',
]

// 不包括up的常驻学生
const getStudentList = () => {
    const { display = [], upId } = getConfig
    return data.filter(i => i.IsReleased[2] && i.IsLimited == 0 && display.findIndex(item => item == i.Id) == -1 && i.Id != upId)
    // IsLimited 0 常驻 1 限定 2 活动
}

const tens = (total = true) => {
    const { tensConfig, upId = 0 } = getConfig
    const { colours = 0.03, gold = 0.185, blue = 0.785, gold1 = 0.97 } = tensConfig
    const result = {
        card: [],
        info: {
            colours: 0,
            gold: 0,
            blue: 0,
            up: 0,
            stone: 0,
            all: 10,
        },
    }
    // 填充9连
    for (let index = 0; index < 9; index++) {
        const random = Math.random() * 100
        if (random < colours * 100) result.info.colours++
        else if (random < (colours + gold) * 100) result.info.gold++
        else if (random < (colours + gold + blue) * 100) result.info.blue++
    }
    // 保底 必定金以上
    const random1 = Math.random() * (colours + gold1) * 100
    if (random1 < colours * 100) result.info.colours++
    else if (random1 < (colours + gold1) * 100) result.info.gold++
    const stone = result.info.colours * 50 + result.info.gold * 10 + result.info.blue
    result.info.stone = stone
    if (upId != 0) { // 非常驻池 计算up
        // 计算up数量
        const up = 0.007
        for (let index = 0; index < result.info.colours; index++) {
            const random2 = Math.random() * colours
            if (random2 < up) {
                result.info.up++
                result.info.colours--
            }
        }
    }
    if (total) result.card = totalCard(result)
    return result
}

const jin = () => {
    const result = {
        card: [],
        info: {
            colours: 0,
            gold: 0,
            blue: 0,
            up: 0,
            stone: 0,
            all: 200,
            firstUp: 0, // 第几个十连出的up
        },
    }
    for (let index = 0; index < 20; index++) {
        const item = tens(false)
        result.info.colours += item.info.colours
        result.info.gold += item.info.gold
        result.info.blue += item.info.blue
        result.info.up += item.info.up
        result.info.stone += item.info.stone
        if (item.info.up && result.info.firstUp == 0) result.info.firstUp = index + 1
    }
    result.card = totalCard(result)
    return result
}

const more = () => {
    const result = {
        card: [],
        info: {
            colours: 0,
            gold: 0,
            blue: 0,
            up: 0,
            stone: 0,
            all: 2000000,
        },
    }
    for (let index = 0; index < 200000; index++) {
        const item = tens()
        result.info.colours += item.info.colours
        result.info.gold += item.info.gold
        result.info.blue += item.info.blue
        result.info.up += item.info.up
        result.info.stone += item.info.stone
    }
    return result
}

const getUpInfo = () => {
    const { upId } = getConfig
    return data.filter(i => i.Id == upId)[0]
}

const totalCard = (result) => {
    const list = getStudentList()
    const blueList = list.filter(i => i.StarGrade === 1)
    const goldList = list.filter(i => i.StarGrade === 2)
    const coloursList = list.filter(i => i.StarGrade === 3)
    for (let index = 0; index < result.info.blue; index++) {
        const item = _.sample(blueList)
        const hasIndex = result.card.findIndex(i => i.id == item.Id)
        if (hasIndex > -1) result.card[hasIndex].num += 1
        else result.card.push({ id: item.Id, name: item.Name, num: 1, star: 1 })
    }
    for (let index = 0; index < result.info.gold; index++) {
        const item = _.sample(goldList)
        const hasIndex = result.card.findIndex(i => i.id == item.Id)
        if (hasIndex > -1) result.card[hasIndex].num += 1
        else result.card.push({ id: item.Id, name: item.Name, num: 1, star: 2 })
    }
    for (let index = 0; index < result.info.colours; index++) {
        const item = _.sample(coloursList)
        const hasIndex = result.card.findIndex(i => i.id == item.Id)
        if (hasIndex > -1) result.card[hasIndex].num += 1
        else result.card.push({ id: item.Id, name: item.Name, num: 1, star: 3 })
    }
    // 排序 先按star排再按num排
    result.card = result.card.sort((a, b) => {
        if (a.star !== b.star) return b.star - a.star
        return b.num - a.num
    })
    if (result.info.up) {
        const upItem = getUpInfo()
        result.card.unshift({ id: upItem.Id, name: upItem.Name, num: result.info.up, star: upItem.StarGrade })
    }
    return result.card
}

const format = (result, list = []) => {
    // list 十连独有的玩家抽卡记录
    const res = ['爱丽丝给sensei拿来了最喜欢的九蓝一金❤']
    if (result.info.all > 10) result.card = result.card.filter(i => i.star == 3)
    result.card.map(i => {
        let item = ''
        for (let index = 0; index < i.star; index++) {
            item += '★ '
        }
        item += `${i.name} x ${i.num}`
        res.push(item)
    })
    res.push(`+${result.info.stone}`)
    if (result.info.firstUp) {
        res.push(`在第${result.info.firstUp}个十连出第一个up`)
    }
    if (result.info.all > 10) {
        const text = _.sample(cxg)
        if (result.info.up == 0) res.push(text)
        else if (result.info.up >= 4) res.push('sensei开了是吧(')
    }
    if (list.length) {
        const cardNumber = list.reduce((pre, t) => pre + t.num, 0)
        const stone = list.reduce((pre, t) => {
            let stoneNum = 0
            if (t.star == 1) stoneNum += t.num
            else if (t.star == 2) stoneNum += 10 * t.num
            else if (t.star == 3) stoneNum += 50 * t.num
            return pre + stoneNum
        }, 0)
        res.push(`当前母猪石：${stone}`)
        res.push(`累计抽卡：${cardNumber}`)
        const coloursList = list.filter(i => i.star == 3)
        if (coloursList.length) {
            res.push(`当前累计三星：`)
            coloursList.map(i => {
                let item = ''
                for (let index = 0; index < i.star; index++) {
                    item += '★ '
                }
                item += `${i.name} x ${i.num}`
                res.push(item)
            })
        }
    }
    return res.join('\n')
}

const updateConfig = (data = {}) => {
    let config = getConfig // 更改config.ts文件
    config = {
        ...config,
        ...data,
        lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss')
    }
    fs.writeFileSync('./external/blue/src/utils/config.json', JSON.stringify(config, null, 4)) // 保存
}

export {
    tens,
    jin,
    more,
    format,
    updateConfig,
}